package com.nomito.minimito.service.impl;

import com.nomito.minimito.entity.ImFriendEntity;
import com.nomito.minimito.entity.ImUserEntity;
import com.nomito.minimito.service.ImFriendService;
import com.nomito.minimito.service.ImUserService;
import com.nomito.minimito.vo.FriendRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nomito.minimito.common.utils.PageUtils;
import com.nomito.minimito.common.utils.Query;

import com.nomito.minimito.dao.ImRequestDao;
import com.nomito.minimito.entity.ImRequestEntity;
import com.nomito.minimito.service.ImRequestService;


@Service("imRequestService")
public class ImRequestServiceImpl extends ServiceImpl<ImRequestDao, ImRequestEntity> implements ImRequestService {

    @Autowired
    ImUserService imUserService;
    @Autowired
    ImFriendService friendService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImRequestEntity> page = this.page(
                new Query<ImRequestEntity>().getPage(params),
                new QueryWrapper<ImRequestEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FriendRequestVo> queryFriendRequestList(Long myId) {

        return baseMapper.queryFriendRequestList(myId);



    }

    @Override
    public void sendFriendRequest(Long myUserId, String username,String msg) {

        ImUserEntity myFriendInfo = imUserService.getOne(new QueryWrapper<ImUserEntity>().eq("username", username));

        ImFriendEntity friendEntity = friendService.getOne(new QueryWrapper<ImFriendEntity>()
                .eq("friend_id", myFriendInfo.getId()).eq("user_id", myUserId));
        if (friendEntity == null){
            //不是好友，进行添加
            ImRequestEntity requestEntity = new ImRequestEntity();
            requestEntity.setSendUserId(myUserId);
            requestEntity.setAcceptUserId(myFriendInfo.getId());
            requestEntity.setRequestMsg(msg);
            this.save(requestEntity);

        }
    }

}